package com.swpu.service_food.controller;


import cn.hutool.core.date.DateUtil;
import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_food.entity.Food;
import com.swpu.service_food.entity.vo.FoodVo;
import com.swpu.service_food.service.FoodRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-01-27
 */
@RestController
@RequestMapping("/service_food/food")
public class FoodRecordController {

    @Autowired
    private FoodRecordService foodRecordService;

    @PostMapping("/addFoodRecord")
    @Encrypt
    @Decrypt
       public  R addFoodRecord(@RequestBody ResultEncryption resultEncryption, FoodVo foodVo, HttpServletRequest request ){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        foodRecordService.addFoodRecord(foodVo,openid);
        return R.ok();
    }

    //获取用户早、晚餐的饮食记录
    @GetMapping("/getUserFoodRecordByType")
    public R getUserFoodRecordByDietType(@RequestParam Integer dietType,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
        List foodRecord = foodRecordService.getFoodRecordeByType(dietType, openid);
        return R.ok().data("foodRecord",foodRecord);
    }
    //获取当天的食物卡路里
    @GetMapping("/getFoodCaloriesByDate")
    public R getFoodCaloriesByDate(HttpServletRequest request,@RequestParam("queryDate") String queryDate){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error().message("用户未登录");
        Integer todayFoodCalories = foodRecordService.getFoodCaloriesByDate(openid, DateUtil.parseDate(queryDate));
        return R.ok().data("todayFoodCalories",todayFoodCalories);
    }
    //删除饮食记录
    @DeleteMapping("/deleteFoodRecordById")
    public R deleteFoodRecordById(@RequestParam String id,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
        foodRecordService.deleteFoodRecordById(id);
        return R.ok();
    }
    //获取当天饮食记录数
    @GetMapping("/getFoodRecordNum")
    public R getFoodRecordNum(HttpServletRequest request){

        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
      Integer foodRecordNum=   foodRecordService.getFoodRecordNum(openid);
      return R.ok().data("foodNum",foodRecordNum);
    }

}

